﻿using IService.Admin;
using Model.Parameter.Admin;

namespace Service.Admin
{
    public class MsgCtrlHandleService : BaseService<TMsgCtrlHandle>, IMsgCtrlHandleService
    {
        public MsgCtrlHandleService(IUnitOfWorkService unitOfWorkService) : base(unitOfWorkService)
        {
        }

        /// <summary>
        /// 添加或修改
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        public async Task<RM_ApiResult> AddOrUpdate(TMsgCtrlHandle par)
        {
            par.HandleTime = DateTime.Now;
            par.AdminId = _CurrentAdmin.Id;

            var result = await AddOrUpdate(par, true);

            if (result > 0)
            {
                await Status<TMsgCtrl>(par.MsgId, 1);

                return _ResultHelper.BuildActionSuccess();
            }
            else
            {
                return _ResultHelper.BuildActionError();
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        public async Task<RM_ApiResult> GetPage(Par_AdminPageBase par)
        {
            var exp = Expressionable.Create<TMsgCtrl>()
                .And(o => o.IsDeleted == false).AndIF(!string.IsNullOrWhiteSpace(par.KeyWord), o => o.Name.Contains(par.KeyWord));

            var where = exp.ToExpression();
            RM_PageReturn<TMsgCtrl> datas = new RM_PageReturn<TMsgCtrl>
            {
                TotalCount = await Count(where),
                Datas = await _DbContext.Queryable<TMsgCtrl>().Includes(u => u.MsgCtrlHandles
                .ToList(), x => x.AdminInfo)
                .Includes(u => u.Customer)
                .Where(where).OrderBy(o => o.Status).OrderByDescending(o => o.Id)                
                .ToPageListAsync(par.PageNo, par.PageSize),
                PageSize = par.PageSize,
                PageNo = par.PageNo
            };

            datas.Datas.ForEach(item =>
            {
                if (item.Customer != null)
                {
                    item.Name = item.Customer.NickName;
                    item.Phone = item.Customer.Phone;
                    item.Email = item.Customer.Email;
                }
                if (item.MsgCtrlHandles.Count > 0)
                {
                    var handle = item.MsgCtrlHandles.Where(h => h.IsDeleted == false).OrderByDescending(h => h.Id).First();
                    if (handle != null)
                    {
                        item.HandleTime = handle.HandleTime;
                        item.HandleDesc = handle.HandleDesc;
                        item.HandleBy = handle.AdminInfo.AdminName;
                        item.HandleAttachment = handle.HandleAttachment;
                        item.HandlePicture = handle.HandlePicture;
                    }
                }

                item.StatusDesc = _EnumHelper.GetDescription((Enum_MsgStatus)item.Status);
                item.SourceDesc = _EnumHelper.GetDescription(item.Source);
            });

            return _ResultHelper.BuildSuccess(datas);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public new async Task<RM_ApiResult> GetItem(int key)
        {
            if (await Any<TMsgCtrl>(key))
            {
                var data = await _DbContext.Queryable<TMsgCtrl>().Includes(u => u.MsgCtrlHandles.Where(h => h.IsDeleted == false).OrderByDescending(h => h.Id)
        .ToList(), x => x.AdminInfo)
                    .Includes(u => u.Customer)
                    .OrderBy(o => o.Status).OrderByDescending(o => o.Id).FirstAsync(o => o.Id == key);

                if (data != null)
                {
                    if (data.Customer != null)
                    {
                        data.Name = data.Customer.NickName;
                        data.Phone = data.Customer.Phone;
                        data.Email = data.Customer.Email;
                    }

                    if (data.MsgCtrlHandles.Count > 0)
                    {
                        var handle = data.MsgCtrlHandles.Where(h => h.IsDeleted == false).OrderByDescending(h => h.Id).First();
                        if (handle != null)
                        {
                            data.HandleTime = handle.HandleTime;
                            data.HandleDesc = handle.HandleDesc;
                            data.HandleBy = handle.AdminInfo.AdminName;
                            data.HandleAttachment = handle.HandleAttachment;
                            data.HandlePicture = handle.HandlePicture;
                        }
                    }

                    data.StatusDesc = _EnumHelper.GetDescription((Enum_MsgStatus)data.Status);
                    data.SourceDesc = _EnumHelper.GetDescription(data.Source);
                }

                return _ResultHelper.BuildSuccess(data);
            }
            else
            {
                return _ResultHelper.BuildError(Enum_SysAttributeType.Misc.NoData);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public async Task<RM_ApiResult> Deleted(int id, bool isDeleted = true)
        {
            await _DbContext.DeleteNav<TMsgCtrl>(u => u.Id == id).Include(u => u.MsgCtrlHandles).ExecuteCommandAsync();
            return _ResultHelper.BuildActionSuccess();
        }
    }
}